<?php
    if(cpx_get_user_role_name() != 'administrator')
    {
        if(admin_url('/index.php') == cpx_current_url() || admin_url('/') == cpx_current_url())
        {
            // redirect to Deals page
            wp_redirect(admin_url('/edit.php'),301);
        }        
        add_action('admin_menu', 'remove_if_non_admin', 999);        
    }
        
	function remove_if_non_admin()
	{
        remove_menu_page('index.php');
		remove_menu_page('upload.php');
        remove_menu_page('edit.php?post_type=page');
        remove_menu_page('edit-comments.php');
        remove_menu_page('tools.php');
		//remove_submenu_page('edit.php', 'edit-tags.php?taxonomy=category');
	}
    
    add_action('admin_head','add_admin_scripts');
    function add_admin_scripts()
    { ?>
        <script type="text/javascript">    
        jQuery(document).ready(function($)
        {   
            $('#last_deactive').val('<?php echo get_option('last_update_deal_status');?>');
            $('#last_active').val('<?php echo get_option('last_active_deal_status');?>');
            
            $('#run_update_deal_now').click(function(){
                var data = {
                    action:'action_admin'
                };
                $.post(ajaxurl, data, function(rs) {
                    alert(rs);
                    $('#last_deactive').val(rs);
                });
            })
            
            $('#run_active_deal_now').click(function(){
                var data = {
                    action:'action_active'
                };
                $.post(ajaxurl, data, function(rs) {
                    alert(rs);
                    $('#last_active').val(rs);
                });
            })
        })
        </script>  
    <?php
    }
    add_action('wp_ajax_action_admin','action_admin_callback');
    function action_admin_callback()
    {
        deactive_expired_or_soon_deals();
        echo date('Y/m/d');
        die();
    }
    
    add_action('wp_ajax_action_active','action_active_callback');
    function action_active_callback()
    {
        active_soon_deals();
        echo date('Y/m/d');
        die();
    }
?>